package com.li.controller;

import com.li.pojo.dto.BusinessDTO;
import com.li.pojo.dto.BusinessReasonDTO;
import com.li.pojo.po.Opportunities;
import com.li.pojo.po.OpportunitiesRecord;
import com.li.pojo.po.PageResult;
import com.li.pojo.po.Result;
import com.li.pojo.vo.AssignmentVo;
import com.li.service.BusinessService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/business")
@Slf4j
public class BusinessController {

    @Autowired
    private BusinessService businessService;

    /**
     * 商机分页
     * @param businessDTO
     * @return
     */
    @GetMapping("/list")
    public PageResult listPage(BusinessDTO businessDTO){
        PageResult pageResult = businessService.listPage(businessDTO);
        return pageResult;
    }

    /**
     * 商机新增
     * @param opportunities
     * @return
     */
    @PostMapping
    public Result listSave(@RequestBody Opportunities opportunities){
        businessService.listSave(opportunities);
        return Result.success();
    }

    /**
     * 公海池分页
     * @param businessReasonDTO
     * @return
     */
    @GetMapping("/pool")
    public PageResult poolPage(BusinessReasonDTO businessReasonDTO){
        PageResult pageResult = businessService.poolPage(businessReasonDTO);
        return pageResult;
    }

    /**
     * 商机详情
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result find(@PathVariable Integer id){
        Opportunities opportunities = businessService.find(id);
        return new Result(200,"操作成功",opportunities);
    }

    /**
     * 商机修改
     * @param opportunities
     * @return
     */
    @PutMapping
    public Result update(@RequestBody Opportunities opportunities){
        businessService.update(opportunities);
        return Result.success();
    }

    /**
     * 商机跟进记录新增
     * @return
     */
    @PostMapping("/record")
    public Result recordAdd(@RequestBody Opportunities opportunities){
        log.info("{}",opportunities);
        businessService.recordAdd(opportunities);
//        businessService.recordAdd(record);
        return Result.success();
    }

    /**
     * 沟通记录展示
     * @param id
     * @return
     */
    @GetMapping("/record/list")
    public Result recordList(@RequestParam("businessId") Long id){
        List<OpportunitiesRecord> records = businessService.recordList(id);
        return new Result(200,"操作成功", records);
    }

    /**
     * 踢回公海
     * @param id
     * @param reason
     * @return
     */
    @PutMapping("/back/{id}/{reason}")
    public Result backReason(@PathVariable Integer id,@PathVariable String reason){
        Integer i = businessService.backReason(id,reason);
        return new Result(200,"操作成功",i);
    }

    /**
     * 捞
     * @param assignmentVo
     * @return
     */
    @PutMapping("/gain")
    public Result reasonGain(@RequestBody AssignmentVo assignmentVo){
        businessService.reasonGain(assignmentVo);
        return new Result(200,"操作成功","");
    }

    /**
     * 商机分配
     * @param assignmentVo
     * @return
     */
    @PutMapping("/assignment")
    public Result Assignment(@RequestBody AssignmentVo assignmentVo){
        businessService.Assignment(assignmentVo);
        return new Result(200,"操作成功","");
    }

}
